package threadlocal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 描述：加锁来解决线程安全问题
 */

public class ThreadLocalNormalUsage03 {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    public String date(int seconds) {
        Date date = new Date(1000L * seconds);
        synchronized (ThreadLocalNormalUsage03.class) {
            // 多个线程不会同时进入这个方法，所以不会出现线程安全问题
            return dateFormat.format(date);
        }
    }
    public static ExecutorService threadPool = Executors.newFixedThreadPool(10);
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            int finalI = i;
            threadPool.submit(() -> {
                String date = new ThreadLocalNormalUsage03().date(finalI);
                System.out.println(date);
            });
        }
        threadPool.shutdown();
    }
}
